package com.epro.dx.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epro.dx.R;
import com.epro.dx.widget.WaitingDialog;
import com.umeng.analytics.MobclickAgent;
import com.weipu.common.util.Logger;

/**
 * Fragment 基类
 * 
 * @author  hjd008
 * @version  [版本号, 2015-1-12]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@SuppressLint("NewApi")
public class BaseFragment extends Fragment
{
    
    /**
     * topbar 左侧内容
     */
    public TextView topbarLeftTv;
    
    /**
     * topbar title
     */
    public TextView topbarTitleTv;
    
    /**
     * topbar 右侧内容
     */
    public TextView topbarrightTv;
    
    /**
     * 加载动画对话框
     */
    private WaitingDialog mWaitingDialog;
    
    protected LinearLayout loadingLayout;
	
	//网络错误界面 和数据为空页面公用
	private LinearLayout errorNetLayout;

	private TextView erroType;//错误页面标题

	private TextView tvEmpty;//错误页面，提示信息
    
    protected View inflate(int layoutResID)
    {
        View view = LayoutInflater.from(getActivity()).inflate(layoutResID, null);
        topbarLeftTv = (TextView)view.findViewById(R.id.topbar_left_tv);
        topbarTitleTv = (TextView)view.findViewById(R.id.topbar_title_tv);
        topbarrightTv = (TextView)view.findViewById(R.id.topbar_right_tv);
        
        loadingLayout = (LinearLayout)view.findViewById(R.id.loading_layout);
        errorNetLayout = (LinearLayout) view.findViewById(R.id.layout_empty);
        erroType = (TextView) view.findViewById(R.id.error_type);
        tvEmpty = (TextView) view.findViewById(R.id.tv_empty);
        if (null != topbarTitleTv)
        {
            topbarTitleTv.setFocusable(true);
            topbarTitleTv.requestFocus();
        }
        return view;
    }
    
    /**  
     * 设置topbar 左侧内容
     * @param isBackIcon 是否需要显示返回图标
     * @param resId  string.xml中的资源来设置按钮的内容 -1即不显示文字
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected View setLeftBtnContent(Boolean isBackIcon, int resId)
    {
        if (!isBackIcon && -1 != resId)
        {
            return null;
        }
        else
        {
            topbarLeftTv.setVisibility(View.VISIBLE);
            if (isBackIcon)
            {
                topbarLeftTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.selector_topbar_back, 0, 0, 0);
            }
            if (-1 != resId)
            {
                topbarLeftTv.setText(resId);
            }
        }
        return topbarLeftTv;
    }
    
    /**  
     * 设置topbar 左侧内容
     * @param resId 通过string.xml中的资源来设置按钮的内容
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected View setRightBtnContent(int resId)
    {
        topbarrightTv.setVisibility(View.VISIBLE);
        topbarrightTv.setText(resId);
        return topbarrightTv;
    }
    
    /**  
     * 设置topbar title
     * @param resId 通过string.xml资源来设置标题
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected View setTitleContent(int resId)
    {
        return setTitleContent(getString(resId));
    }
    
    /**  
     * 设置topbar title
     * @param resId 通过string.xml资源来设置标题
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected View setTitleContent(String title)
    {
        topbarTitleTv.setText(title);
        return topbarTitleTv;
    }
    
    /** 
     *  显示加载对话框
     * @see [类、类#方法、类#成员]
     */
    protected void showWaitingDialog()
    {
        if (mWaitingDialog == null)
        {
            mWaitingDialog = new WaitingDialog(getActivity(), getString(R.string.loading), true);
            mWaitingDialog.setOnDismissListener(new OnDismissListener() {
				
				@Override
				public void onDismiss(DialogInterface arg0) {
					mWaitingDialog=null;
				}
			});
        }
        mWaitingDialog.show();
    }
    
    /**  
     * 隐藏加载对话框
     * @see [类、类#方法、类#成员]
     */
    protected void hideWaitingDialog()
    {
        if (mWaitingDialog == null||!mWaitingDialog.isShowing())
        {
            return;
        }
        mWaitingDialog.dismiss();
    }
    
    
    @Override
    public void onAttach(Activity activity)
    {
        Logger.d(this.getClass().getSimpleName(), "onAttach" + SystemClock.currentThreadTimeMillis());
        super.onAttach(activity);
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        Logger.d(this.getClass().getSimpleName(), "onCreate" + SystemClock.currentThreadTimeMillis());
        super.onCreate(savedInstanceState);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        Logger.d(this.getClass().getSimpleName(), "onCreateView" + SystemClock.currentThreadTimeMillis());
        return super.onCreateView(inflater, container, savedInstanceState);
    }
    
    @Override
    public void onResume()
    {
        super.onResume();
        //统计页面
        MobclickAgent.onPageStart(this.getTag());
    }
    
    @Override
    public void onPause()
    {
        super.onPause();
        //统计页面
        MobclickAgent.onPageEnd(this.getTag());
    }
    
    @Override
    public void onDestroyView()
    {
        Logger.d(this.getClass().getSimpleName(), "onDestroyView" + SystemClock.currentThreadTimeMillis());
        super.onDestroyView();
    }
    
    protected void showContentLayout(View view){
    	if(null!=view){
    		view.setVisibility(View.VISIBLE);
    	}
    	if(null!=errorNetLayout){
    		errorNetLayout.setVisibility(View.GONE);
        }
        if(null!=loadingLayout){
        	loadingLayout.setVisibility(View.GONE);
        }
    }
    
    protected void showErrorNetLayout(View view,int erroType,int tvEmpty){
    	if(null!=view){
    		view.setVisibility(View.GONE);
    	}
    	if(null!=errorNetLayout){
    		this.erroType.setText(erroType);
    		this.tvEmpty.setText(tvEmpty);
    		errorNetLayout.setVisibility(View.VISIBLE);
    	}
    	if(null!=loadingLayout){
    		loadingLayout.setVisibility(View.GONE);
    	}
    }
    protected void showErrorNetLayout(View view){
        if(null!=view){
            view.setVisibility(View.GONE);
        }
        if(null!=errorNetLayout){
            errorNetLayout.setVisibility(View.VISIBLE);
        }
        if(null!=loadingLayout){
            loadingLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 展示加载页面
     * @param view
     */
    protected void showLoadingLayout(View view){
    	if(null!=view){
    		view.setVisibility(View.GONE);
    	}
    	if(null!=errorNetLayout){
    		errorNetLayout.setVisibility(View.GONE);
        }
        if(null!=loadingLayout){
        	loadingLayout.setVisibility(View.VISIBLE);
        }
    }
    
    /**
     * 展示加载页面
     * @param view
     */
    protected void hideLoadingLayout(){
        if(null!=loadingLayout){
        	loadingLayout.setVisibility(View.GONE);
        }
    }
}
